<?php
/**
 * XML MISMO report
 *
 * This library formats an incoming XML string and outputs the XML
 * in MISMO format. Support for MISMO 2.x and beta support for 3.0
 * are included, check the xsl files located in templates/xml.
 * @package Reports
 */

/**
 *
 */
require_once(AD_SERVER_PATH . '/pkgs/init.php');
require_once(AD_SERVER_PATH . '/pkgs/server.php');
require_once(AD_SERVER_PATH . '/reports/xml_report.php');

/**
 * XML MISMO report
 *
 * This library formats an incoming XML string and outputs the XML
 * in MISMO format. Support for MISMO 2.x and beta support for 3.0
 * are included, check the xsl files located in templates/xml.
 * @package Reporting
 */
class CXML_mismo extends CXML_report
{
	/**
	 * Constructor
	 *
	 * Called when class is initialized,
	 * pass xsl and xsd file locations as arguments.
	 * @param string $xsl_file
	 * @param string $xsd_file
	 */
	public function __construct($xsl_file, $xsd_file)
	{
		$this->xsl = $xsl_file;
		$this->xsd = $xsd_file;
	}

	/**
	 * Outputs MISMO XML report
	 *
	 * Converts XML string to MISMO format,
	 * and outputs XML results to screen.
	 */
	public function get_report()
	{
		$results = $this->get_data();

		$dom = new DOMDocument;
		$dom->load($this->xsl);

		$proc = new XSLTProcessor;
		$proc->importStyleSheet($dom);

		$dom->loadXML($results);
		$xml = $proc->transformToXML($dom);

		$xml = $this->_convert_special_chars($xml);
		$xml = $this->_base64_encode_imgs($xml, $results);

		$xml = '<?xml version="1.0"?>' . '<!DOCTYPE VALUATION_RESPONSE SYSTEM "templates/xml/mismo.2_6.dtd">' . $xml;

		if ((AD_VALIDATE_MISMO_XML) && (!($this->_validate_report($xml))))
		{
			$xml  = '<?xml version="1.0"?>';
			$xml .= '<result> XML is invalid. </result>';
		}

		header ('content-type: text/xml');
		echo $xml;
	}

	/**
	 * Convert special characters
	 *
	 * This method is needed so that XSL parsing works properly.
	 * @param string $xml
	 * @return string $xml
	 */
	private function _convert_special_chars($xml)
	{
		$xml = str_replace('"', '&quot;', $xml);

		$xml = str_replace('@&quot;@', '"', $xml);
		$xml = str_replace('@&lt;@', '<', $xml);
		$xml = str_replace('@&gt;@', '>', $xml);
		$xml = str_replace('@@', ' ', $xml);

		return $xml;
	}

	/**
	 * Base64 encodes images and injects them into MISMO results
	 *
	 * This is needed so that binary images can be stored in XML.
	 * @param string $xml
	 * @param array $results
	 * @return string $xml
	 */
	private function _base64_encode_imgs($xml, $results)
	{
		$server_obj = new CServer();

		$data_xml = simplexml_load_string($results);

		foreach ($data_xml->rows->row AS $row)
		{
			$image  = $server_obj->get_img($row->ml_number, 0);
			$output = (is_array($image[0])) ? htmlspecialchars(base64_encode($image[0]['Data'])) : '';
			$xml    = str_replace('@' . $row->ml_number . '@', $output, $xml);
		}
		return $xml;
	}

	/**
	 * Validate report
	 *
	 * Checks MISMO XML against xsd / dtd file
	 * to verfiy that it meets MISMO standards.
	 * @param string $xml
	 * @return bool
	 */
	private function _validate_report($xml)
	{
		$dom = new DOMDocument;
		$dom->loadXML($xml);

		return $dom->validate();
	}
}
